home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / Thread Manager / Thread Manager 2.1.1d1+ / Threads.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-28  |  9.7 KB  |  267 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        Threads.h
  3.  
  4.      Copyright:    © 1984-1995 by Apple Computer, Inc.
  5.                  All rights reserved.
  6.  
  7.      Version:    Universal Interfaces 2.0a3+  ETO #17, MPW prerelease.  Friday, April 28, 1995. 
  8.  
  9.      Bugs?:        If you find a problem with this file, send the file and version
  10.                  information (from above) and the problem description to:
  11.  
  12.                      Internet:    apple.bugs@applelink.apple.com
  13.                      AppleLink:    APPLE.BUGS
  14.  
  15. */
  16.  
  17. #ifndef __THREADS__
  18. #define __THREADS__
  19.  
  20.  
  21. #ifndef __ERRORS__
  22. #include <Errors.h>
  23. #endif
  24. /*    #include <ConditionalMacros.h>                                */
  25.  
  26. #ifndef __MEMORY__
  27. #include <Memory.h>
  28. #endif
  29. /*    #include <Types.h>                                            */
  30. /*    #include <MixedMode.h>                                        */
  31.  
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35.  
  36. #if GENERATINGPOWERPC
  37. #pragma options align=mac68k
  38. #endif
  39.  
  40. #ifdef __CFM68K__
  41. #pragma lib_export on
  42. #endif
  43.  
  44. typedef unsigned short ThreadState;
  45.  
  46.  
  47. enum {
  48.     kReadyThreadState            = 0,
  49.     kStoppedThreadState            = 1,
  50.     kRunningThreadState            = 2
  51. };
  52.  
  53. /* Error codes have been meoved to Errors.(pah) */
  54. /* Thread environment characteristics */
  55. typedef void *ThreadTaskRef;
  56.  
  57. /* Thread characteristics */
  58. typedef unsigned long ThreadStyle;
  59.  
  60.  
  61. enum {
  62.     kCooperativeThread            = 1L << 0,
  63.     kPreemptiveThread            = 1L << 1
  64. };
  65.  
  66. /* Thread identifiers */
  67. typedef unsigned long ThreadID;
  68.  
  69.  
  70. enum {
  71.     kNoThreadID                    = 0,
  72.     kCurrentThreadID            = 1,
  73.     kApplicationThreadID        = 2
  74. };
  75.  
  76. /* Options when creating a thread */
  77. typedef unsigned long ThreadOptions;
  78.  
  79.  
  80. enum {
  81.     kNewSuspend                    = (1 << 0),
  82.     kUsePremadeThread            = (1 << 1),
  83.     kCreateIfNeeded                = (1 << 2),
  84.     kFPUNotNeeded                = (1 << 3),
  85.     kExactMatchThread            = (1 << 4)
  86. };
  87.  
  88. /* Information supplied to the custom scheduler */
  89. struct SchedulerInfoRec {
  90.     unsigned long                    InfoRecSize;
  91.     ThreadID                        CurrentThreadID;
  92.     ThreadID                        SuggestedThreadID;
  93.     ThreadID                        InterruptedCoopThreadID;
  94. };
  95. typedef struct SchedulerInfoRec SchedulerInfoRec;
  96.  
  97. typedef SchedulerInfoRec *SchedulerInfoRecPtr;
  98.  
  99. /* Prototype for thread's entry (main) routine */
  100. typedef void *voidPtr;
  101.  
  102. #if !defined(__CFM68K__)
  103.  
  104. /*
  105.     The following ProcPtrs cannot be interchanged with UniversalProcPtrs because
  106.     of differences between 680x0 and PowerPC runtime architectures with regard to
  107.     the implementation of the Thread Manager.
  108. */
  109.  
  110. typedef pascal voidPtr (*ThreadEntryProcPtr)(void *threadParam);
  111. /* Prototype for custom thread scheduler routine */
  112. typedef pascal ThreadID (*ThreadSchedulerProcPtr)(SchedulerInfoRecPtr schedulerInfo);
  113. /* Prototype for custom thread switcher routine */
  114. typedef pascal void (*ThreadSwitchProcPtr)(ThreadID threadBeingSwitched, void *switchProcParam);
  115. /* Prototype for thread termination notification routine */
  116. typedef pascal void (*ThreadTerminationProcPtr)(ThreadID threadTerminated, void *terminationProcParam);
  117. /* Prototype for debugger NewThread notification */
  118. typedef pascal void (*DebuggerNewThreadProcPtr)(ThreadID threadCreated);
  119. /* Prototype for debugger DisposeThread notification */
  120. typedef pascal void (*DebuggerDisposeThreadProcPtr)(ThreadID threadDeleted);
  121. /* Prototype for debugger schedule notification */
  122. typedef pascal ThreadID (*DebuggerThreadSchedulerProcPtr)(SchedulerInfoRecPtr schedulerInfo);
  123.  
  124. #else
  125.  
  126. /*
  127.     The following UniversalProcPtrs are for CFM-68k compatiblity with
  128.     the implementation of the Thread Manager.
  129. */
  130.  
  131. typedef UniversalProcPtr ThreadEntryProcPtr;
  132. typedef UniversalProcPtr ThreadSchedulerProcPtr;
  133. typedef UniversalProcPtr ThreadSwitchProcPtr;
  134. typedef UniversalProcPtr ThreadTerminationProcPtr;
  135. typedef UniversalProcPtr DebuggerNewThreadProcPtr;
  136. typedef UniversalProcPtr DebuggerDisposeThreadProcPtr;
  137. typedef UniversalProcPtr DebuggerThreadSchedulerProcPtr;
  138.  
  139. /*
  140.     Please note that your source will have to be modifed in the following
  141.     fashion in order to support classic 68k, PowerPC and CFM-68k.
  142.     
  143.             #if defined(__CFM68K__)
  144.             
  145.                 myErr = NewThread(    kCooperativeThread, 
  146.                                     ( ThreadEntryProcPtr )NewThreadEntryProc( SortPictsThreadEntry ), 
  147.                                     ( void* )this, 
  148.                                     20000,
  149.                                     kCreateIfNeeded,
  150.                                     ( void** )nil,
  151.                                     &tempThreadInfo );
  152.                                         
  153.             #else
  154.             
  155.                 myErr = NewThread(    kCooperativeThread, 
  156.                                     ( ThreadEntryProcPtr )SortPictsThreadEntry, 
  157.                                     ( void* )this, 
  158.                                     20000,
  159.                                     kCreateIfNeeded,
  160.                                     ( void** )nil,
  161.                                     &tempThreadInfo );
  162.                                         
  163.             #endif
  164.             
  165.     The same ort of changes will be necessary for all of the ProcPtrs used
  166.     in the Thread Manager.
  167.     
  168. */
  169.  
  170. enum {
  171.     uppThreadEntryProcInfo = kPascalStackBased
  172.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(void*))),
  173.     uppThreadSchedulerProcInfo = kPascalStackBased
  174.          | RESULT_SIZE(SIZE_CODE(sizeof(ThreadID)))
  175.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SchedulerInfoRecPtr))),
  176.     uppThreadSwitchProcInfo = kPascalStackBased
  177.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID)))
  178.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(void*))),
  179.     uppThreadTerminationProcInfo = kPascalStackBased
  180.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID)))
  181.          | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(void*))),
  182.     uppDebuggerNewThreadProcInfo = kPascalStackBased
  183.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID))),
  184.     uppDebuggerDisposeThreadProcInfo = kPascalStackBased
  185.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(ThreadID))),
  186.     uppDebuggerThreadSchedulerProcInfo = kPascalStackBased
  187.          | RESULT_SIZE(SIZE_CODE(sizeof(ThreadID)))
  188.          | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(SchedulerInfoRecPtr)))
  189. };
  190.  
  191. #define NewThreadEntryProc(userRoutine)        \
  192.         (ThreadEntryProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadEntryProcInfo, GetCurrentArchitecture())
  193. #define NewThreadSchedulerProc(userRoutine)        \
  194.         (ThreadSchedulerProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadSchedulerProcInfo, GetCurrentArchitecture())
  195. #define NewThreadSwitchProc(userRoutine)        \
  196.         (ThreadSwitchProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadSwitchProcInfo, GetCurrentArchitecture())
  197. #define NewThreadTerminationProc(userRoutine)        \
  198.         (ThreadTerminationProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppThreadTerminationProcInfo, GetCurrentArchitecture())
  199. #define NewDebuggerNewThreadProc(userRoutine)        \
  200.         (DebuggerNewThreadProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerNewThreadProcInfo, GetCurrentArchitecture())
  201. #define NewDebuggerDisposeThreadProc(userRoutine)        \
  202.         (DebuggerDisposeThreadProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerDisposeThreadProcInfo, GetCurrentArchitecture())
  203. #define NewDebuggerThreadSchedulerProc(userRoutine)        \
  204.         (DebuggerThreadSchedulerProcPtr) NewRoutineDescriptor((ProcPtr)(userRoutine), uppDebuggerThreadSchedulerProcInfo, GetCurrentArchitecture())
  205.  
  206. #endif
  207.  
  208. /* Thread Manager routines */
  209. extern pascal OSErr CreateThreadPool(ThreadStyle threadStyle, short numToCreate, Size stackSize)
  210.  THREEWORDINLINE(0x303C, 0x0501, 0xABF2);
  211. extern pascal OSErr GetFreeThreadCount(ThreadStyle threadStyle, short *freeCount)
  212.  THREEWORDINLINE(0x303C, 0x0402, 0xABF2);
  213. extern pascal OSErr GetSpecificFreeThreadCount(ThreadStyle threadStyle, Size stackSize, short *freeCount)
  214.  THREEWORDINLINE(0x303C, 0x0615, 0xABF2);
  215. extern pascal OSErr GetDefaultThreadStackSize(ThreadStyle threadStyle, Size *stackSize)
  216.  THREEWORDINLINE(0x303C, 0x0413, 0xABF2);
  217. extern pascal OSErr ThreadCurrentStackSpace(ThreadID thread, unsigned long *freeStack)
  218.  THREEWORDINLINE(0x303C, 0x0414, 0xABF2);
  219. extern pascal OSErr NewThread(ThreadStyle threadStyle, ThreadEntryProcPtr threadEntry, void *threadParam, Size stackSize, ThreadOptions options, void **threadResult, ThreadID *threadMade)
  220.  THREEWORDINLINE(0x303C, 0x0E03, 0xABF2);
  221. extern pascal OSErr DisposeThread(ThreadID threadToDump, void *threadResult, Boolean recycleThread)
  222.  THREEWORDINLINE(0x303C, 0x0504, 0xABF2);
  223. extern pascal OSErr YieldToThread(ThreadID suggestedThread)
  224.  THREEWORDINLINE(0x303C, 0x0205, 0xABF2);
  225. extern pascal OSErr YieldToAnyThread(void)
  226.  FOURWORDINLINE(0x42A7, 0x303C, 0x0205, 0xABF2);
  227. extern pascal OSErr GetCurrentThread(ThreadID *currentThreadID)
  228.  THREEWORDINLINE(0x303C, 0x0206, 0xABF2);
  229. extern pascal OSErr GetThreadState(ThreadID threadToGet, ThreadState *threadState)
  230.  THREEWORDINLINE(0x303C, 0x0407, 0xABF2);
  231. extern pascal OSErr SetThreadState(ThreadID threadToSet, ThreadState newState, ThreadID suggestedThread)
  232.  THREEWORDINLINE(0x303C, 0x0508, 0xABF2);
  233. extern pascal OSErr SetThreadStateEndCritical(ThreadID threadToSet, ThreadState newState, ThreadID suggestedThread)
  234.  THREEWORDINLINE(0x303C, 0x0512, 0xABF2);
  235. extern pascal OSErr SetThreadScheduler(ThreadSchedulerProcPtr threadScheduler)
  236.  THREEWORDINLINE(0x303C, 0x0209, 0xABF2);
  237. extern pascal OSErr SetThreadSwitcher(ThreadID thread, ThreadSwitchProcPtr threadSwitcher, void *switchProcParam, Boolean inOrOut)
  238.  THREEWORDINLINE(0x303C, 0x070A, 0xABF2);
  239. extern pascal OSErr SetThreadTerminator(ThreadID thread, ThreadTerminationProcPtr threadTerminator, void *terminationProcParam)
  240.  THREEWORDINLINE(0x303C, 0x0611, 0xABF2);
  241. extern pascal OSErr ThreadBeginCritical(void)
  242.  THREEWORDINLINE(0x303C, 0x000B, 0xABF2);
  243. extern pascal OSErr ThreadEndCritical(void)
  244.  THREEWORDINLINE(0x303C, 0x000C, 0xABF2);
  245. extern pascal OSErr SetDebuggerNotificationProcs(DebuggerNewThreadProcPtr notifyNewThread, DebuggerDisposeThreadProcPtr notifyDisposeThread, DebuggerThreadSchedulerProcPtr notifyThreadScheduler)
  246.  THREEWORDINLINE(0x303C, 0x060D, 0xABF2);
  247. extern pascal OSErr GetThreadCurrentTaskRef(ThreadTaskRef *threadTRef)
  248.  THREEWORDINLINE(0x303C, 0x020E, 0xABF2);
  249. extern pascal OSErr GetThreadStateGivenTaskRef(ThreadTaskRef threadTRef, ThreadID threadToGet, ThreadState *threadState)
  250.  THREEWORDINLINE(0x303C, 0x060F, 0xABF2);
  251. extern pascal OSErr SetThreadReadyGivenTaskRef(ThreadTaskRef threadTRef, ThreadID threadToSet)
  252.  THREEWORDINLINE(0x303C, 0x0410, 0xABF2);
  253.  
  254. #ifdef __CFM68K__
  255. #pragma lib_export off
  256. #endif
  257.  
  258. #if GENERATINGPOWERPC
  259. #pragma options align=reset
  260. #endif
  261.  
  262. #ifdef __cplusplus
  263. }
  264. #endif
  265.  
  266. #endif /* __THREADS__ */
  267.